package cn.crm.controller;

import cn.crm.pojo.BaseDict;
import cn.crm.pojo.Customer;
import cn.crm.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @RequestMapping("/customer")
    public String clist(Model model,
                        @RequestParam(value="cust_name",required = false) String cust_name,
                        @RequestParam(value="cust_source",required = false) String cust_source,
                        @RequestParam(value="cust_industry",required = false) String cust_industry,
                        @RequestParam(value="cust_level",required = false) String cust_level){
        List<Customer> list = customerService.find(cust_name, cust_source, cust_industry,cust_level);

        List<BaseDict> list1 = customerService.list("002");
        List<BaseDict> list2 = customerService.list("001");
        List<BaseDict> list3 = customerService.list("006");

        model.addAttribute("list",list);
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);

        model.addAttribute("cust_name",cust_name);
        model.addAttribute("cust_source",cust_source);
        model.addAttribute("cust_industry",cust_industry);
        model.addAttribute("cust_level",cust_level);
        return "index";
    }

    @RequestMapping("/custadd")
    public String add(Model model){
        List<BaseDict> list1 = customerService.list("002");
        List<BaseDict> list2 = customerService.list("001");
        List<BaseDict> list3 = customerService.list("006");

        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);

        return "custadd";
    }

    @RequestMapping("/docustadd")
    public String doadd(Customer customer){
        customerService.add(customer);
        return "redirect:/customer";
    }

    //1.显示修改页面
    @RequestMapping("/custupdate")
    public String update(@RequestParam("id") Integer id, Model model){
        Customer customer = customerService.findById(id);
        model.addAttribute("customer",customer);

        List<BaseDict> list1 = customerService.list("002");
        List<BaseDict> list2 = customerService.list("001");
        List<BaseDict> list3 = customerService.list("006");

        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);

        return "custupdate";
    }

    //2.执行修改操作
    @RequestMapping("/docustupdate")
    public String doupdate(Customer customer){
        customerService.update(customer);
        return "redirect:/customer";
    }

    @RequestMapping("/custdel")
    public String del(@RequestParam("id") Integer id){
        customerService.del(id);
        return "redirect:/customer";
    }

}
